<template>
    <div>
        <div id="headerbox">
            <div id="left" @touchstart="back">
                <span class="fa fa-angle-left"></span>
            </div>
            <div id="right">
                <input type="text" v-model="keyWord" @input="wordChange"><img src="../assets/ser.png" alt="" @touchstart="searchClick">
            </div>
        </div>
    </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex';
export default {
    data: function() {
        return {
            keyWord: "",
            timer: null
        }
    },
    methods: {
        back: function() {
            history.back();
        },
        wordChange: function() {
            // console.log(this.$route.params.msg);
            this.$emit("showClick", "istrue")
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
            this.timer = setTimeout(function() {
                // console.log("发送请求");
                
                    this.$store.dispatch("goodsSearch", { params: { 
                        word: this.keyWord,
                         a: this.$route.params.msg
                } })
                        .then(res => {
                            // console.log(res);
                            // this.searchResult = res;                  
                        })
                
                this.timer = null;
            }.bind(this), 700);
        },
        searchClick: function() {
            this.$emit("reqs", "isRequest")
            if (this.keyWord) {
                this.$store.dispatch("goodsSearch", { params: { word: this.keyWord } })
                    .then(res => {
                        // console.log(res);

                    })
            }

        }
    }
}
</script>

<style scoped>
#headerbox {
    position: fixed;
    top: 0;
    display: flex;
    width: 100vw;
    height: 40px;
    background-color: #f2f2f2;
    border-bottom: solid 1px #dcdcdc;
}

#left {
    width: 20vw;
    padding-left: 20px;
    height: 40px;
    line-height: 35px;
    font-size: 30px;
}

#right {
    width: 80vw;
    padding-left: 15px;

    line-height: 35px;
    position: relative;
    vertical-align: middle;
}

#right input {
    width: 70vw;
    height: 30px;
    border-radius: 5px;
    border: 1px solid #DCDCDC;
    background-color: white;
}

#right img {
    height: 20px;
    position: absolute;
    top: 12px;
    right: 25px;
}
</style>